package com.zsc.controller.user;

import com.zsc.dto.OrdersSubmitDTO;
import com.zsc.dto.ShoppingCartDTO;
import com.zsc.dto.ShoppingCartPageQueryDTO;
import com.zsc.result.PageResult;
import com.zsc.result.Result;
import com.zsc.service.OrdersService;
import com.zsc.service.ShoppingCartService;
import com.zsc.vo.OrderSubmitVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author Enkidu
 * @Date 2024/6/21 21:07
 */
@RestController
@Api(tags = "用户端 购物车相关接口")
@RequestMapping("/user/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private OrdersService ordersService;

    /**
     * 查看购物车
     *
     * @return
     */

//    public Result<List<ShoppingCart>> list() {
//        List<ShoppingCart> list = shoppingCartService.showShoppingCart();
//        return Result.success(list);
//    }
    @GetMapping("/list")
    @ApiOperation("查看购物车")
    public Result<PageResult> page(ShoppingCartPageQueryDTO shoppingCartPageQueryDTO) {
        PageResult pageResult = shoppingCartService.showShoppingCart(shoppingCartPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 新增/添加 菜品/套餐 至购物车
     *
     * @param shoppingCartDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加购物车")
    public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("添加购物车，商品信息：{}", shoppingCartDTO);
        shoppingCartService.addShoppingCart(shoppingCartDTO);
        return Result.success();
    }

    /**
     * 减少/删除 菜品/套餐 至购物车
     *
     * @param shoppingCartDTO
     * @return
     */
    @PostMapping("/sub")
    @ApiOperation("删除购物车")
    public Result sub(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("删除购物车，商品信息：{}", shoppingCartDTO);
        shoppingCartService.subShoppingCart(shoppingCartDTO);
        return Result.success();
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @ApiOperation("清空购物车")
    @DeleteMapping("/clean")
    public Result clean() {
        shoppingCartService.cleanShoppingCart();
        return Result.success();
    }

    /**
     * 提交购物车生成订单
     *
     * @param ordersSubmitDTO
     * @return
     */
    @PostMapping("/submit")
    @ApiOperation("提交购物车生成订单")
    public Result<OrderSubmitVO> submit(OrdersSubmitDTO ordersSubmitDTO) {
        log.info("提交购物车生成订单，订单信息：{}");
        OrderSubmitVO orderSubmitVO = ordersService.submitOrder(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }
}
